import express, { Request, Response } from "express";
import { body } from "express-validator";

import { validateRequest } from "../middlewares/validate-request";
import { NotFoundError } from "../errors/not-found-error";
import { requireAuth } from "../middlewares/require-auth";
import { NotAuthorizedError } from "../errors/not-authorized-error";

import { Ticket } from "../models/ticket";

const router = express.Router();

router.put(`/api/tickets/:id`, requireAuth, [
        body('title').not().isEmpty().withMessage("标题不能为空"),
        body('price').isFloat({ gt: 0 }).withMessage("价格有问题")
], validateRequest, async (req: Request, res: Response) => {
	const ticket = await Ticket.findById(req.params.id);
	if (!ticket) throw new NotFoundError();
	if (ticket.userId !== req.currentUser!.id) throw new NotAuthorizedError();
	ticket.set({
		title: req.body.title,
		price: req.body.price
	});
	await ticket.save();
	res.send(ticket);
});

export { router as updateTicketRouter };
